package com.luoqy.speedy.core.base.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.luoqy.speedy.common.ConfigManage;
import com.luoqy.speedy.common.FirstCreateBase;
import com.luoqy.speedy.core.cache.CacheServiceImpl;
import com.luoqy.speedy.data.DataBase;
import com.luoqy.speedy.util.Result;


@Controller
@RequestMapping("/first")
public class FirstController {
	@Autowired
	DataSource dataSource;
	
	@RequestMapping("/webFirst")
	public String webFirst(Model model) throws IOException{
		//这里应处理加载首页参数
			return "/view/index.html";
		
	}
	/**
	 * 后台登录校验是否第一次进入
	 * @throws IOException
	 */
	@RequestMapping("/checkFirst")
	public String checkFirst(Model model) throws IOException{
		//通过数据库查询是否能查询到
		Connection conn=null;
		try {
			CacheServiceImpl cache=new CacheServiceImpl();
			@SuppressWarnings("unchecked")
			Map<String,String> map=(Map<String, String>) cache.getCacheDataByKey("jdbc");
			conn = (Connection) DriverManager.getConnection(map.get("url"), map.get("account"),map.get("password"));
			conn.close();
			return "WEB-INF/login";
		} catch (Exception e) {
			try {
				if (conn != null){
					conn.close();
				};
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
			}
			// TODO: handle exception
			String message="欢迎第一次进入speedy管理系统,请进行数据信息初始化操作(可咨询客服人员QQ2629192332)";
			model.addAttribute("message", message);
			model.addAttribute("data", ConfigManage.findProperties("jdbc"));
			return "WEB-INF/index";
		}
		
	}
	
	@RequestMapping("/testDatabase")
	@ResponseBody
	public Result testDatabase(String password,String account,String url){
		Connection conn=null;
		try {
			 conn = (Connection) DriverManager.getConnection(url,account,password);
			conn.close();
			return Result.success("链接成功",null);
		} catch (Exception e) {
			// TODO: handle exception
				try {
					if (conn != null)conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
				}
			return Result.error("链接失败："+e.getMessage());
		}
	}
	@RequestMapping("/updateDataBase")
	@ResponseBody
	public Result updateDataBase(HttpServletRequest req){
		Enumeration<String> parames=req.getParameterNames();
		Map<String,String> map=new HashMap<String,String>();
		while(parames.hasMoreElements()){
			//调用nextElement方法获得元素
			String name = (String)parames.nextElement();
			String value=req.getParameter(name);
			if(!"id".equals(name)){
				if(null!=value&&!"".equals(value)){
					map.put(name, value);
				}
			}
		}
		try {
			FirstCreateBase.linkTest(map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return Result.error(e.getMessage());
		}
		//更新缓存的同时，应同时更新配置文件
		String result=ConfigManage.updateProperties("jdbc", map);
		if("SUCCESS".equals(result)){
			CacheServiceImpl cache=new CacheServiceImpl();
			cache.putCache("jdbc", map, 0);
			if(DataBase.RestartService()){
				return Result.success("初始化成功,即将跳转登录页面~",null);
			}else{
				return Result.error("初始化数据信息失败");
			}
			
		}else{
			return Result.error("初始化失败："+result);
		}
	}
}
